<?php
/**
* Django's templating engine (partial) PHP implementation.
* provides some template tags support
* @link http://djangoproject.com
* this file is from a webappkit package (kit)
* @link http://webappkit.net
* @package djtpl
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* tag node : for loop
* <code>{% for i in array %}
* - {{ i }}
* {% endfor %}
* </code>
* <p>provides vars within loop :</p>
* <dl><dt>forloop.counter</dt>
* <dt>forloop.counter0</dt>
* <dt>forloop.revcounter0, forloop.revcounter0</dt>
* <dd>reverse counters</dd>
* <dt>forloop.first, forloop.last</dt>
* <dd>booleans</dd></dl>
*/
class djtplTagNode_for extends djtplTagNode {

	/**
	* builds itself from tokens
	* @param djtplToken &$selfToken
	* @param array &$nextTokens djangoTplToken
	* @access public
	* @return bool
	*/
	function buildFromTokens(&$selfToken, &$nextTokens) {
		// $this->token=&$selfToken;
		$def=$selfToken->tagdef;
		$regex='([^ ]+) in ([^ ]+)( reversed)?';
		if (ereg($regex,$def,$search)) {
			$this->alias=$search[1];
			$this->source=$search[2];
			$this->reversed=$search[3]?true:false;
		} else {
			// error !
			trigger_error('invalid structure for tag for : '.$def);
		}
		$this->buildChildNodesFromTokens($nextTokens);
		return true;
	}/**/

	/**
	* is the node atomic (has no children)
	* @access public
	* @return bool
	*/
	function isAtomic() {
		return false;
	}/**/
	
	/**
	* renders as string
	* @param djangoTplContext
	* @access public
	* @return string
	*/
	function render(&$context) {
		$rendered='';
		// getting list item keys
		if (!$keys=$context->getKeys($this->source)) {
			//trigger_error("djangoTpl: for: {$this->source} has no childs");
			return $rendered;
		} // print_r($keys);
		// reverse loop ?
		if ($this->reversed)
			$keys=array_reverse($keys);
		//echo '<pre>before loop : '; print_r($context); echo '</pre>';
		// looping items
		$counter=0; $total=count($keys);
		foreach ($keys as $key) {
			// setting alias
			$context->setPathAlias($this->source.'.'.$key,$this->alias);
			// setting loop vars
			$counter++;
			$context->setPath('forloop',array());
			$context->setPath('forloop.counter',$counter);
			$context->setPath('forloop.counter0',$counter-1);
			$context->setPath('forloop.revcounter',$total-$counter+1);
			$context->setPath('forloop.revcounter0',$total-$counter);
			$context->setPath('forloop.first',$counter==1);
			$context->setPath('forloop.last',$counter==$total);
			//echo '<pre>'; print_r($context); echo '</pre>';
			// rendering content
			$rendered.=$this->renderChildNodes($context);
		}
		return $rendered;
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var string tag name
	*/
	var $tagname='for';

	/**
	 * @var string temporary key for items within loop
	 */
	var $alias;

	/**
	 * @var string path of looped array
	 */
	var $source;

}/* end of djtpltagnode_for class */
?>
